package TUILayer;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.SystemColor;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import ControlLayer.EmployeeCtr;
import ModelLayer.Employee;
import ModelLayer.EmployeeList;
import TUILayer.homeView;

import com.jgoodies.forms.factories.DefaultComponentFactory;

import javax.swing.JTextPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class loginView extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	private static EmployeeList employeeList;
	private static EmployeeCtr employeeCtr;
	private JTextPane textPane;
	public String role;
	private JTextField textFieldLogin;
	public boolean isFound;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					employeeList = EmployeeList.getInstance();
					employeeCtr = new EmployeeCtr();
					employeeCtr.createEmployeeRoot();
					
					loginView frame = new loginView();
					frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public loginView() {
		setResizable(false);
		setTitle("Logitech LogIn");
		setBackground(SystemColor.activeCaption);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 319, 182);
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.inactiveCaption);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("");
		label.setBounds(5, 138, 293, 0);
		contentPane.add(label);
		
		JLabel lblSystemUserLogin = new JLabel("User LogIn");
		lblSystemUserLogin.setFont(new Font("Source Sans Pro Light", Font.PLAIN, 14));
		lblSystemUserLogin.setBounds(13, 17, 147, 14);
		contentPane.add(lblSystemUserLogin);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(15, 46, 46, 14);
		contentPane.add(lblName);
		
		textField = new JTextField();
		textField.setBounds(85, 43, 198, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(15, 75, 64, 14);
		contentPane.add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(85, 72, 198, 20);
		contentPane.add(passwordField);
		
		JLabel label_1 = new JLabel("_______________________________________________________________________________");
		label_1.setBounds(-213, 21, 557, 14);
		contentPane.add(label_1);
		
		JButton btnLogin = new JButton("LogIn");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				employeeList = EmployeeList.getInstance();
				
				Long userId = Long.valueOf(textField.getText());
				
				
				employeeCtr.getEmployeeId(userId);
				isFound = employeeList.isFound();
				
				
				if(isFound = false)
				{
					textPane.setText("Not Found!");
				}
				else
				{
					try{
				   homeView s = new homeView();
				   s.setVisible(true);
				   s.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				    
	  	    	  	} catch (Exception ex) {
						// TODO Auto-generated catch block
						ex.printStackTrace();
					}
				   
				
				}
				
			
				
				
			}
		});
		btnLogin.setBounds(194, 115, 89, 23);
		contentPane.add(btnLogin);
		
		JLabel label_2 = new JLabel("_______________________________________________________________________________");
		label_2.setBounds(-12, 90, 557, 14);
		contentPane.add(label_2);
		
		textPane = new JTextPane();
		textPane.setBackground(SystemColor.inactiveCaption);
		textPane.setEditable(false);
		textPane.setBounds(5, 115, 179, 20);
		contentPane.add(textPane);
		
		textFieldLogin = new JTextField();
		textFieldLogin.setEnabled(false);
		textFieldLogin.setEditable(false);
		textFieldLogin.setBounds(170, 11, 79, 20);
		contentPane.add(textFieldLogin);
		textFieldLogin.setColumns(10);
	}
}
